<?php
// Library File
include 'lib/library.php';

$title = 'Message Center';
$subtitle = 'Message Center';

login_check();

	$content .= '
		<div class="inbox">
			<div class="linksbox">
	';
			
			// The pages GET variable
			// changes what is displayed
			$mode = mysql_real_escape_string($_GET['mode']);
			if(empty($mode)){
				unset($mode);
			}
			
			// Make box active if neccessary
			if(!isset($mode)){
				$content .= '<div class="box active" '.click_refresh().'><P>Inbox</P></div>'."\n";
				$subtitle .= ' - Inbox';
			}else{
				$content .= '<div class="box" '.click_refresh().'><P>Inbox</P></div>'."\n";
			}
			
			// Make box active if neccessary
			if($mode == 'sent'){
				$content .= '<div class="box active" '.click_get('mode', 'sent').'><P>Sent</P></div>'."\n";
				$subtitle .= ' - Sent';
			}else{
				$content .= '<div class="box" '.click_get('mode', 'sent').'><P>Sent</P></div>'."\n";
			}

			// Make box active if neccessary
			if($mode == 'addressbook'){
				$content .= '<div class="box active" '.click_get('mode', 'addressbook').'><P>Address Book</P></div>'."\n";
				$subtitle .= ' - Address Book';
			}else{
				$content .= '<div class="box" '.click_get('mode', 'addressbook').'><P>Address Book</P></div>'."\n";
			}
			
				// Spacer to push compose button to the right
				$content .= '<div class="spacer"><P></P></div>'."\n";
			
			// Make box active if neccessary
			if($mode == 'compose'){
				$content .= '<div class="box compose active" '.click_get('mode', 'compose').'><P>Compose Message</P></div>'."\n";
				$subtitle .= ' - Compose Message';
			}else{
				$content .= '<div class="box compose" '.click_get('mode', 'compose').'><P>Compose Message</P></div>'."\n";
			}
	
	// Close linksbox div
	$content .= '
			</div>
	';
	
	// Display the toolbar
	// for every mode except compose
	if($mode != 'compose' && !isset($_GET['read'])){
		$content .= '
			<script language="javascript" type="text/javascript" src="message_selectors.js"></script>
		';
		$content .= '
			<div class="toolbar">
			<label id="test" for="selectmenu">Select:</label><select id="selectmenu" name="selectmenu" onchange="selector(this.value)">
					<option value="">---</option>
					<option value="None">None</option>
					<option value="Read">Read</option>
					<option value="Unread">Unread</option>
					<option value="All">All</option>
				</select>
			<UL>
			<li><P>Mark as Unread</P></li>
			<li><P>Mark as Read</P></li>
			<li><P>Delete</P></li>
			</UL>
			
			</div>
		';
	
	}

	/*
	** Start the Modes Section
	*/
	
	// Set to normal inbox mode
	// retrieve and display messages
	if(!isset($mode) && !isset($_GET['read'])){
		
		$content .= '
			<script language="javascript" type="text/javascript" src="scripts/update_message.js"></script>
			<script language="javascript" type="text/javascript" src="scripts/delete_message.js"></script>
		';
		
		$query = mysql_query("SELECT * FROM messages WHERE recipient='$m_user_id' ORDER BY id DESC");
		
		$num_of_messages = mysql_num_rows($query);
		
		// See if messages for user exist
		if($num_of_messages > 0){
			
			$content .= '
				<form name="checks" id="checks">
			';
			
			while($message_array = mysql_fetch_array($query)){
				$user_query = mysql_query("SELECT name FROM users WHERE user_id='{$message_array['sender']}'");
				$user_array = mysql_fetch_assoc($user_query);
				
				$u_name = $user_array['name'];
			
				// If message is read
				if($message_array['readbool']){
					$content .='
						<div class="message read">
					';
					$readbool = 'read';
					$img_title = 'Mark as Unread';
					
				// Else if message isn't read
				}else{
					$content .='
						<div class="message unread">
					';
					$readbool = 'unread';
					$img_title = 'Mark as Read';
				}
				
				$content .= '
					<div class="check">
						<img title="'.$img_title.'"src="'.$addr.'/imgs/'.$readbool.'.png" onclick="update_message('.$message_array['message_id'].')" />
						<input type="checkbox" name="check" value="'.$message_array['id'].'" />
					</div>
					<div class="userdate">
						<P><a href="'.$addr.'/profile.php?id='.$message_array['sender'].'">'.$u_name.'</a></P>
						<P>'.$message_array['date'].'</P>
					</div>
					<div class="titlesnip">
						<P><a href="'.$addr.'/messages.php?read='.$message_array['message_id'].'">'.nl2br(stripslashes($message_array['title'])).'</a></P>
				';
					if(strlen($message_array['content']) > 30){
						$content .= '
							<P><a href="'.$addr.'/messages.php?read='.$message_array['message_id'].'">'.substr(stripslashes($message_array['content']),0,30).'...</a></P>
						';
					}else{
						$content .= '
							<P><a href="'.$addr.'/messages.php?read='.$message_array['message_id'].'">'.stripslashes($message_array['content']).'</a></P>
						';
					}
				$content .= '
					</div>
					<div class="delete">
						<img title="Delete" src="'.$addr.'/imgs/delete.png" onclick="delete_message('.$message_array['message_id'].')" />
					</div>
				</div>
				';
			}
			
			$content .= '
				</form>
			';
		}
		
	// Set to sent mode
	// retrieve and display sent messages
	}else if($mode == 'sent' && !isset($_GET['read'])){
	
		$content .= '
			<script language="javascript" type="text/javascript" src="scripts/delete_message.js"></script>
		';
		$query = mysql_query("SELECT * FROM messages WHERE sender='$m_user_id' ORDER BY id DESC");
		
		$num_of_messages = mysql_num_rows($query);
		
		// See if messages for user exist
		if($num_of_messages > 0){
		
			$content .= '
				<form name="checks" id="checks">
			';
		
			while($message_array = mysql_fetch_array($query)){
				$user_query = mysql_query("SELECT name FROM users WHERE user_id='{$message_array['recipient']}'");
				$user_array = mysql_fetch_assoc($user_query);
				
				$u_name = $user_array['name'];
				
				$content .='
					<div class="message read">
						<div class="check">
							<input type="checkbox" name="check" value="'.$message_array['id'].'" />
						</div>
						<div class="userdate">
							<P><a href="'.$addr.'/profile.php?id='.$message_array['recipient'].'">To: '.$u_name.'</a></P>
							<P>'.$message_array['date'].'</P>
						</div>
						<div class="titlesnip">
							<P><a href="'.$addr.'/messages.php?mode=sent&read='.$message_array['message_id'].'">'.stripslashes($message_array['title']).'</a></P>
					';
						if(strlen($message_array['content']) > 30){
							$content .= '
								<P><a href="'.$addr.'/messages.php?mode=sent&read='.$message_array['message_id'].'">'.substr(stripslashes($message_array['content']),0,30).'...</a></P>
							';
						}else{
							$content .= '
								<P><a href="'.$addr.'/messages.php?mode=sent&read='.$message_array['message_id'].'">'.stripslashes($message_array['content']).'</a></P>
							';
						}
					$content .= '
						</div>
						<div class="delete">
							<img title="Delete" src="'.$addr.'/imgs/delete.png" onclick="delete_message('.$message_array['message_id'].')" />
						</div>
					</div>
				';
			}
			
			$content .= '
				</form>
			';
		}

	// Set to read mode
	}else if(isset($_GET['read'])){
		$message_id = $_GET['read'];
		
		// Retrieve message row from database
		$query = mysql_query("SELECT * FROM messages WHERE message_id='$message_id'");
		$message_exists = mysql_num_rows($query);
		
		// If the message doesn't exist refresh the page
		if(!$message_exists){
			refresh(this_page());
		}
		
		$message_array = mysql_fetch_assoc($query);
		
		// Get sender's name
		$sender_query = mysql_query("SELECT name FROM users WHERE user_id='{$message_array['sender']}'");
		$sender_array = mysql_fetch_assoc($sender_query);
		
		// Add a user to your temporary friend network
		// if not in temporary or permanent friend network already
		$friend_query = mysql_query("SELECT id FROM friends WHERE (friend1='{$message_array['sender']}' AND friend2='$m_user_id') OR (friend1='$m_user_id' AND friend2='{$message_array['sender']}')");
		
		// if the user does not exist in your friend network
		// make a temporary entry
		if(mysql_num_rows($friend_query) == 0){
			// creation_date increases this date by a month
			$creation_date = date("m/d/Y",mktime(0,0,0,date("m")+1,date("d"),date("Y"),0));
			$query = mysql_query("INSERT INTO friends VALUES('','$m_user_id','{$message_array['sender']}','contact','$creation_date',0)");
		}
		
		if($mode == 'sent'){
			$fromto = 'To';
		}else{
			$fromto = 'From';
		}
		
		$content .= '
			<div class="message_box">
				<div class="titleinfo">
		';
					$title = stripslashes($message_array['title']);
					if(substr($title,0,3) == 'RE:'){
						$reply_title = $title;
					}else{
						$reply_title = 'RE: '.$title;
					}
					
		$content .= '
					<P>'.$fromto.': '.$sender_array['name'].'</P>
					<P>"'.nl2br($title).'"</P>
					<P>'.$message_array['date'].'</P>
					
					<P class="reply" OnClick="parent.location=\''.$addr.this_page().'?mode=compose&to='.$sender_array['name'].'&subject='.$title.'\'">Reply</p>
				</div>
				<div class="content">
					<hr />
					<P>'.nl2br(stripslashes($message_array['content'])).'</P>
				</div>
			</div>
		';
		
		// Mark message as read
		$update_query = mysql_query("UPDATE messages SET readbool='1' WHERE message_id='{$_GET['read']}' LIMIT 1") or die(mysql_error());
		
	// Set to compose mode
	}else if($mode == 'compose'){
		
		$content .= '
		
			<div class="compose_box">
				<div class="top" id="compose_box">
		';
				$recipient = $_GET['to'];
				$subject = $_GET['subject'];
				// Handles the form
				include $rel_addr.'/handle/compose_handle.php';
				
		$recipient_id_array = mysql_fetch_assoc(mysql_query("SELECT user_id FROM users WHERE name='$recipient'"));
		$recipient_id = $recipient_id_array['user_id'];
				
		// Add a user to your temporary friend network
		// if not in temporary or permanent friend network already
		$friend_query = mysql_query("SELECT friend1,friend2 FROM friends WHERE (friend1='$recipient_id' AND friend2='$m_user_id') OR (friend1='$m_user_id' AND friend2='$recipient_id')");	
		
		
		// if the user does not exist in your friend network
		// make a temporary entry
		if(mysql_num_rows($friend_query) == 0){
		
			// creation_date increases this date by a month
			$creation_date = date("m/d/Y",mktime(0,0,0,date("m")+1,date("d"),date("Y"),0));
			$query = mysql_query("INSERT INTO friends VALUES('','$m_user_id','$recipient_id','contact','$creation_date',0)");
		}
		
				
		$content .= '
				</div>
				<form action="'.this_page().'?mode=compose" method="POST">
					<div class="container">
						<label for="recipient">To: </label>
						<input type="text" size="20" id="recipient" name="recipient" value="'.$recipient.'" /></div>
					<div class="container">
						<label for="title">Subject: </label>
						<input type="text" size="20" id="subject" name="subject" value="'.stripslashes(stripslashes($subject)).'" /></div>
					<div class="container">
						<label for="message_content">Message: </label>
						<textarea id="message_content" name="message_content">'.stripslashes(nl2br(stripslashes($message_content))).'</textarea>
					</div>
					<div class="container">
						<input type="submit" name="send" class="submit" value="Send" />
					</div>
				</form>
			</div>
		';
	}
	
	$content .= '
		</div>
	';
/*
**   P U T    V A R S
**    O N    P A G E
*/
	head($title,$style,$scripts);
	body($header,$personal,$subtitle,$content,$navigation);
	foot($footer,$dbc);
?>
